<!--门诊费用列表-->
<template>
	<view>
		<view class="top_select_box">
			<view class="top_select">
				<view class="left"><b @click="selectPerson">{{userData.xming0}}</b>的门诊处方</view>
				<view class="edit_icon left" @click="selectPerson"></view>
			</view>
		</view>
		<view class="box_content">
			<ul>
				<li v-for="(item,index) in dataList" :key="index" @click="selectRecord(index)" :style="item.styles">
					<view style="padding-bottom: 10px;">
						<view class="numbers">
							<h4>{{item.ylmc00}} {{item.jfdjh0}}</h4>
						</view>
						<view>
							<p>诊断日期：{{item.zdrq00}}</p>
						</view>
						<view>
							<p>开单医生：{{item.ysmc00}}</p>
						</view>
						<view>
							<p>科室：{{item.ghksmc}}</p>
						</view>
						<view>
							<p>合计费用：{{item.bcfyze}}元</p>
						</view>
					</view>
					<view style="">
						<hr />
					</view>
					<view class="btns list_footer">
						<!-- <button class="box_btn pay_btn" @click.stop="payment(item)">微信支付</button> -->
						<button class="box_btn cancel" @click.stop="detail(item)">查看详情</button>
					</view>
				</li>
			</ul>
			<view style="height: 90px;position: relative;bottom: 0px;">
				<view class="footer">
					<view class="total_money">总计金额:<b>{{total}}</b></view>
					<button class="box_btn pay_btn" @click.stop="payment()">微信支付</button>
				</view>
			</view>

		</view>
		<personList styles="display:none;" ref="personList" v-on:checkedPerson="getDatas"></personList>
	</view>
</template>

<script>
	import personList from "../../modules/personList.vue";
	import pay from "@/tool/pay.js";
	export default {
		data() {
			return {
				total: 0,
				out_trade_no: "",
				userData: {
					"xming0": "默认用户"
				},
				dataList: [],
				checkItem: []
			}
		},
		components: {
			personList
		},
		onLoad(option) {
			if (option.kh0000) {
				//查询相关人的信息
				this.login(option.kh0000,()=>{
					this.init()
				});
			}else{
				this.init();
			}
		},
		methods: {
			init() {
				uni.showLoading({
					title: "数据加载中...",
					mask: true
				})
				this.out_trade_no = "WXCLINIC" + new Date().getTime();
				let user = uni.getStorageSync("user");
				this.userData = user;
				//查看门诊处方单
				let params = {
					"kh0000": user.kh0000,
					"klx000": "3",
					"patid0": user.patid0,
					"hzsfbh": "0",
					"xming0": user.xming0,
					"ywlsh0": "" + new Date().getTime(),
					"funid": "F07.06.02.01"
				};
				this.$postJson("/qnyesb/qnyesbproxy", params, res => {
					if (res.state == 0) {
						let data = res.data.retrieve;
						for (let item in data) {
							data[item].styles = "";
						}
						this.dataList = data;
					} else {
						this.dataList = [];
						let _this = this;
						uni.showModal({
							title: "温馨提示",
							content: res.message + "~点击确定返回首页", //queryRes.message
							mask: true,
							cancelText: "其他就诊人",
							success(showRes) {
								if (showRes.confirm) {
									uni.switchTab({
										url: "../index/index"
									})
								} else {
									_this.selectPerson();
								}
							}
						})
					}
					uni.hideLoading();
				})
			},
			login(kh0000,fun) {
				let data = {
					"kh0000": kh0000,
					"klx000": "3",
					"ywlsh0": "" + new Date().getTime(),
					"funid": "F07.02.02.01"
				}
				uni.showLoading({
					mask: true,
					title: "加载中..."
				})
				this.$postJson("/qnyesb/qnyesbproxy", data, res => {
					//判断用户不存在档案信息则进行跳转
					if (res.state == 0) {
						let user = res.data;
						uni.setStorageSync("user", res.data);
						fun();
					} else {
						uni.navigateTo({
							url: "../bindInfo/bindInfo?kh0000=" + kh0000
						})
					}
					uni.hideLoading();
				})
			},
			selectRecord(index) {
				//记录选中，进入详情页
				let checkItem = this.checkItem;
				if (this.dataList[index].styles) {
					this.dataList[index].styles = "";
					checkItem.forEach((item, i) => {
						if (item == index) {
							checkItem.splice(i, 1); // 从下标 i 开始, 删除 1 个元素
						}
					})
				} else {
					this.dataList[index].styles = "background:#e7e7e7;";
					checkItem.push(index);
				}
				//总算
				let total = 0;
				for (let item in checkItem) {
					let data = this.dataList[checkItem[item]];
					let bcfyze = parseFloat(data.bcfyze);
					total += bcfyze;
				}
				this.total = total;
				console.log(total);
			},
			payment() {
				var payJson = {
					"out_trade_no": this.out_trade_no, //后台生成，唯一性
					"description": "门诊处方缴费",
					"notify_url": "http://127.0.0.1", //异步回调地址，后台配置
					"amount": {
						"total": (parseFloat(this.total) * 100), //(parseFloat(this.params.ghzlf0) *100
						"currency": "CNY"
					},
					"payer": {
						"openid": uni.getStorageSync("openid")
					}
				};
				pay.prePay(payJson, success => {
					uni.showLoading({
						mask: true,
						title: "等待处理中..."
					})
					let checkItem = this.checkItem;
					let retrieves = [];
					for (let item in checkItem) {
						let retrieveData = {};
						let itemData = this.dataList[checkItem[item]];
						let jfdjh0 = itemData.jfdjh0;
						let djje00 = itemData.bcfyze;
						let yllb00 = "";
						if (itemData.yllb00) {
							yllb00 = itemData.yllb00;
						}
						retrieveData.yllb00 = yllb00;
						retrieveData.jfdjh0 = jfdjh0;
						retrieveData.djje00 = djje00;
						retrieves.push(retrieveData);
					}
					let user = this.userData;
					let params = {
						kh0000: user.sfzh00,
						klx000: "3",
						patid0: user.patid0,
						hzsfbh: "0",
						xming0: user.xming0,
						ddh000: this.out_trade_no,
						bcfyze: "" + this.total,
						yhjyxx: JSON.stringify(JSON.stringify(success)),
						ywlsh0: "" + new Date().getTime(),
						zffs00: "2",
						jsddh0: success.prepay_id,
						retrieve: JSON.stringify(retrieves),
						funid: "F07.06.02.03"
					}
					this.$postJson("/qnyesb/qnyesbproxy", params, res => {
						if (res.state == 0) {
							uni.showModal({
								title: "系统提示",
								content: "缴费成功,是否现在返回首页",
								icon: "success",
								success(showRes) {
									if (showRes.confirm) {
										uni.switchTab({
											url: "../index/index"
										})
									} else {
										this.init();
									}
								}
							})
							//刷新页面
						} else {
							uni.showModal({
								title: "温馨提示",
								title: res.message + ",10分钟会自动退款，若未自动退款，请联系工作人员",
								icon: "error",
								success(showRes) {
									if (showRes.confirm) {
										this.init();
									} else {
										this.init();
									}
								}
							})
						}
						uni.hideLoading();
					})
				}, error => {

				})
			},
			detail(item) {
				let user = this.userData;
			},
			selectPerson() {
				this.$refs.personList.selectPerson();
			},
			getDatas(item) {
				this.userData = item;
				this.init();
			},
		}
	}
</script>

<style>
	.box_content {
		width: 96%;
		margin: 0 auto;
	}

	.box_content .numbers {
		border-bottom: 1px solid #F0F0F0;
	}

	.box_content p {

		color: #999999;
	}

	.box_content hr {
		border: 1px solid #F0F0F0;
	}

	.box_content ul li {
		width: 89%;
		background-color: #FFFFFF;
		padding: 20px;
		margin-top: 10px;
		display: inline-block;
		line-height: 30px;
		padding-bottom: 10px;
	}

	.box_content .btns {
		margin-top: 10px;
	}

	.box_content .btns .box_btn {
		width: 94px;
		height: 30px;
		border-radius: 15px;
		font-size: 14px;
		border: none;
		float: right;
		line-height: 30px;
		margin-right: 10px;
	}

	.box_content .btns .pay_btn {
		background-color: #3692FF;
		color: #FFFFFF;
		margin-right: 0px;
	}

	.list_footer {
		padding-top: 5px;
	}

	.footer {
		width: 100%;
		position: fixed;
		height: 80px;
		line-height: 80px;
		bottom: 0;
		left: 0;
		background-color: #FFFFFF;
		box-shadow: 5px 0px 5px #464646;
	}

	.footer .total_money {
		float: left;
		margin-left: 20px;
	}

	.footer .box_btn {
		width: 120px;
		height: 40px;
		line-height: 40px;
		font-size: 16px;
		background-color: #00A500;
		color: #FFFFFF;
		float: right;
		margin-right: 20px;
		margin-top: 20px;
	}
</style>
